/**
 * @author yangwen
 * 全局鼠标效果类
 * 控制了鼠标的显示，替换，清除等功能
 */	
package newx.utils
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import newx.BaseObject;
	import newx.StageInfo;
	import newx.com.ResManager;

	public class MouseHandle extends BaseObject
	{
		private static var _self	:MouseHandle		=	null;
		private var followIcon		:Sprite				=	new Sprite();
		
		private var _data			:Object				=	null;
		
		private var _view			:DisplayObject		=	null;
		
		private var dragObj		:Object				=	new Object;
		public function MouseHandle()
		{
			super();
			Init();
		}
		
		public static function getInstance():MouseHandle
		{
			if(_self == null)
			{
				_self	=	new MouseHandle();
			}
			return _self;
		}
		
		/**
		 * 设置跟随 
		 * @param target
		 * @param isCopy
		 * @param data
		 * 
		 */		
		public function setMouseDrag(target:DisplayObject,isCopy:Boolean	=	true , data:Object = null,ScaleX:Number = 0.5 , ScaleY:Number = 0.5):void
		{
			if(followIcon.stage == null)
			{
				throw new Error("未在舞台初始化后使用。")
				return;
			}
			
			if(target == null)
			{
				return;
			}
			
			while(followIcon.numChildren)	followIcon.removeChildAt(0);
			
			
			
			StageInfo.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			
			if(isCopy)
			{
				_view	=	ResManager.copy(target);
			}
			else
			{
				_view	=	target;
			}
			
			var rec:Rectangle	=	target.getBounds(StageInfo.stage);
			
			_view.width			=	rec.width;
			_view.height		=	rec.height;
			
			_view.alpha			=	.5
			
			followIcon.addChild(_view);
			
			followIcon.x		=	StageInfo.stage.mouseX - followIcon.width	* ScaleX;
			followIcon.y		=	StageInfo.stage.mouseY - followIcon.height	* ScaleY; 
			
			followIcon.startDrag();
			_data	=	data;
		}
		private function onMouseUP(e:MouseEvent):void
		{
			if(followIcon.stage == null)
			{
				throw new Error("未在舞台初始化后使用。")
			}
			while(followIcon.numChildren)	followIcon.removeChildAt(0);
			followIcon.stopDrag();
			StageInfo.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			dragObj["mc"].filters=[];
		}
		/**
		 *拖动的图片 
		 * @return 
		 * 
		 */		
		public function get DragObject():Object
		{
			return dragObj["target"];
		}
		private function onMouseMove(e:MouseEvent):void
		{
			var mc:DisplayObject=dragObj["mc"];
			var target:DisplayObject=dragObj["target"];
			var isCopy:Boolean =dragObj["isCopy"];
			var data:Object =dragObj["data"];
			
//			StageInfo.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUP);
			if(mc.hitTestPoint(StageInfo.stage.mouseX,StageInfo.stage.mouseY))
			{
				return;
			}
			mc.filters = [ FilterUtil.WhiteGlowFilter ];
			if(isCopy)
			{
				_view	=	ResManager.copy(target);
			}
			else
			{
				_view	=	target;
			}
			
			var rec:Rectangle	=	target.getBounds(StageInfo.stage);
			
			_view.width			=	rec.width;
			_view.height		=	rec.height;
			
			_view.alpha			=	.5
			
			followIcon.addChild(_view);
			
			followIcon.x		=	StageInfo.stage.mouseX - followIcon.width * .5;
			followIcon.y		=	StageInfo.stage.mouseY - followIcon.height * .5;
			
			followIcon.startDrag();
			_data	=	data;
		}
		/**
		 *设置拖动  鼠标离开MC后开始执行拖 
		 * @param mc      
		 * @param target  
		 * @param isCopy
		 * @param data
		 * 
		 */		
		public function setItem(mc:DisplayObject,target:DisplayObject,isCopy:Boolean	=	true ,data:Object = null):void
		{
			dragObj["mc"] = mc;
			dragObj["target"]=target;
			dragObj["isCopy"]=isCopy;
			dragObj["data"]=data;
			if(followIcon.stage == null)
			{
				throw new Error("未在舞台初始化后使用。")
				return;
			}
//			
			if(target == null)
			{
				return;
			}
//			
			while(followIcon.numChildren)	followIcon.removeChildAt(0);
//			
			if(target.hitTestObject(mc))
			{
				StageInfo.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
				StageInfo.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUP);
				return;
			}
		}
		/**
		 * 设置拖动 
		 * @param target
		 * @param isCopy
		 * @param data
		 * 
		 */		
		public function setDrag(target:DisplayObject,isCopy:Boolean	=	true , data:Object = null):void
		{
			if(followIcon.stage == null)
			{
				throw new Error("未在舞台初始化后使用。")
				return;
			}
			
			if(target == null)
			{
				return;
			}
			
			while(followIcon.numChildren)	followIcon.removeChildAt(0);
			
			
			
			StageInfo.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			
			if(isCopy)
			{
				_view	=	ResManager.copy(target);
			}
			else
			{
				_view	=	target;
			}
			
			var rec:Rectangle	=	target.getBounds(StageInfo.stage);
			
			_view.width			=	rec.width;
			_view.height		=	rec.height;
			
			_view.alpha			=	.5
			
			followIcon.addChild(_view);
			
			var point : Point = target.localToGlobal( new Point( target.x, target.y ) );
			
			followIcon.x		=	point.x;
			followIcon.y		=	point.y;
			
			followIcon.startDrag();
			_data	=	data;
		}
		
//		public function setFollow(target:DisplayObject,dragMode:Boolean	=	true	,isCopy:Boolean	=	true , data:Object = null):void
//		{
//			
//		}
		
		private function onMouseUp(e:MouseEvent):void
		{
			if(followIcon.stage == null)
			{
				throw new Error("未在舞台初始化后使用。")
			}
			while(followIcon.numChildren)	followIcon.removeChildAt(0);
			followIcon.stopDrag();
		}
		
		public function removeDrag():void
		{
			if(followIcon.stage == null)
			{
				throw new Error("未在舞台初始化后使用。")
			}
			while(followIcon.numChildren)	followIcon.removeChildAt(0);
			followIcon.stopDrag();
			_data	=	null;
			_view	=	null;
		}
		
		public function get targetData():Object
		{
			return _data;
		}
		
		public function get view():DisplayObject
		{
			return _view;
		}
		
		private function Init():void
		{
			followIcon.focusRect					=	false;
			followIcon.tabEnabled					=	false;
			followIcon.mouseChildren				=	false;
			followIcon.mouseEnabled					=	false;
			
			StageInfo.stage.addChild(followIcon);
		}
	}
}